package tum0r.generate_code.database;

import tum0r.utils.extension.StringExtension;
import tum0r.utils.misc.GetFields;
import tum0r.utils.sql.MySQL;
import tum0r.webengine.config.DatabaseConfig;
import tum0r.webengine.model.dao.PageModel;
import tum0r.webengine.utils.misc.Log;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 生成时间: 2021-06-22 16:18:16
 */
@SuppressWarnings("unused")
public class DB {
    public static Write _Write;

    public static class Write {
        private static MySQL sql;

        public Write() {
            if (DatabaseConfig.CONNECT_WRITE_DATABASE) {
                try {
                    sql = new MySQL(DatabaseConfig.DATABASE_VERSION);
                    if (sql.open(DatabaseConfig.WRITE_HOST, DatabaseConfig.WRITE_PORT, DatabaseConfig.DATABASE_NAME, DatabaseConfig.WRITE_USERNAME, DatabaseConfig.WRITE_PASSWORD)) {
                        Log.info("Write 数据库连接成功");
                    } else {
                        DatabaseConfig.CONNECT_WRITE_DATABASE = false;
                        Log.error("Write 数据库连接失败");
                    }
                } catch (SQLException | ClassNotFoundException throwable) {
                    Log.error("Write 数据库连接崩溃");
                    Log.error(throwable);
                }
            }
        }

        public F1ag _F1ag = new F1ag();

        public static class F1ag {
            public void getInsertSQL(StringBuilder command, ArrayList<Object> parameters, tum0r.model.database.F1ag target) {
                command.append("INSERT INTO `F1ag`(`ProblemID`, `Fla9`) VALUES(");
                for (int i = 0; i <= 1; i++) {
                    command.append("@p").append(parameters.size() + i);
                    if (i != 1) {
                        command.append(", ");
                    }
                }
                command.append(");\n");
                parameters.add(target.ProblemID);
                parameters.add(target.Fla9);
            }

            public boolean insert(tum0r.model.database.F1ag target) throws Exception {
                StringBuilder command = new StringBuilder();
                ArrayList<Object> parameters = new ArrayList<>(2);
                getInsertSQL(command, parameters, target);
                return sql.update(command.toString(), parameters.toArray()) > 0;
            }

            public boolean delete(String where, Object... parameters) throws Exception {
                if (StringExtension.isNullOrOnlyWhiteSpace(where)) {
                    throw new Exception("WHERE must not null or only white space");
                }
                return sql.update("DELETE FROM `F1ag` " + where, parameters) > 0;
            }

            public boolean delete(int ProblemID) throws Exception {
                return delete("WHERE `ProblemID`=@p0", ProblemID);
            }

            public void getUpdateSQL(StringBuilder command, ArrayList<Object> parameters, String where, tum0r.model.database.F1ag target, tum0r.generate_code.database.enums.F1agFields... fields) throws Exception {
                command.append("UPDATE `F1ag` SET ");
                if (fields == null || fields.length == 0) {
                    fields = tum0r.generate_code.database.enums.F1agFields.getAllArray();
                }

                GetFields getFields = new GetFields();
                int fieldCount = fields.length - 1;
                for (int i = 0; i <= fieldCount; i++) {
                    tum0r.generate_code.database.enums.F1agFields field = fields[i];
                    command.append("`").append(field.name()).append("`").append("=").append("@p").append(parameters.size());
                    if (i != fieldCount) {
                        command.append(", ");
                    }
                    Class<?> targetFieldClass = getFields.getField(target.getClass(), field.name()).getType();
                    Object value = getFields.getFieldValue(target, field.name());
                    if (targetFieldClass.isEnum()) {
                        if (value != null) {
                            value = ((Enum) value).ordinal();
                        } else {
                            value = 0;
                        }
                    }
                    parameters.add(value);
                }
                boolean whereFlag = StringExtension.isNotNullOrEmpty(where);
                command.append(String.format("%s%s", whereFlag ? " " : "", whereFlag ? where : "")).append(";");
            }

            public boolean update(tum0r.model.database.F1ag target, String where, tum0r.generate_code.database.enums.F1agFields... fields) throws Exception {
                if (StringExtension.isNullOrOnlyWhiteSpace(where)) {
                    throw new Exception("where不能为null或空字符串");
                }
                StringBuilder command = new StringBuilder();
                ArrayList<Object> parameters = new ArrayList<>();
                getUpdateSQL(command, parameters, where, target, fields);
                return sql.update(command.toString(), parameters.toArray()) > 0;
            }

            public boolean update(tum0r.model.database.F1ag target, tum0r.generate_code.database.enums.F1agFields... fields) throws Exception {
                return update(target, "`ProblemID`=" + target.ProblemID, fields);
            }

            public boolean exist(String where, Object... parameters) throws Exception {
                return sql.selectValue("SELECT EXISTS (SELECT 1 FROM `F1ag` " + where + ")", Boolean.class, parameters);
            }

            public boolean exist(int ProblemID) throws Exception {
                return exist("WHERE `ProblemID`=@p0", ProblemID);
            }

            public ArrayList<tum0r.model.database.F1ag> query(tum0r.generate_code.database.enums.F1agFields[] fields, String where, Object... parameters) throws Exception {
                StringBuilder command = new StringBuilder();
                command.append("SELECT ");
                if (fields == null || fields.length == 0) {
                    fields = tum0r.generate_code.database.enums.F1agFields.getAllArray();
                }
                int count = fields.length - 1;
                for (int i = 0; i <= count; i++) {
                    command.append(String.format("`%s`", fields[i].name()));
                    if (i != count) {
                        command.append(", ");
                    }
                }
                command.append(" FROM `F1ag`");
                if (StringExtension.isNotNullOrOnlyWhiteSpace(where)) {
                    command.append(" ").append(where);
                }
                command.append(";");
                ArrayList<tum0r.model.database.F1ag> result = sql.selectObjects(command.toString(), tum0r.model.database.F1ag.class, parameters);
                return result != null ? result : new ArrayList<>();
            }

            public tum0r.model.database.F1ag queryFirst(tum0r.generate_code.database.enums.F1agFields[] fields, String where, Object... parameters) throws Exception {
                ArrayList<tum0r.model.database.F1ag> temp = query(fields, where, parameters);
                return temp.size() > 0 ? temp.get(0) : null;
            }

            public tum0r.model.database.F1ag queryByProblemID(tum0r.generate_code.database.enums.F1agFields[] fields, int ProblemID) throws Exception {
                ArrayList<tum0r.model.database.F1ag> result = query(fields, "WHERE `ProblemID`=@p0", ProblemID);
                return result.size() != 0 ? result.get(0) : null;
            }
        }

        public User _User = new User();

        public static class User {
            public void getInsertSQL(StringBuilder command, ArrayList<Object> parameters, tum0r.model.database.User target) {
                command.append("INSERT INTO `User`(`ID`, `Username`, `Password`) VALUES(");
                for (int i = 0; i <= 2; i++) {
                    command.append("@p").append(parameters.size() + i);
                    if (i != 2) {
                        command.append(", ");
                    }
                }
                command.append(");\n");
                parameters.add(target.ID);
                parameters.add(target.Username);
                parameters.add(target.Password);
            }

            public int insert(tum0r.model.database.User target) throws Exception {
                StringBuilder command = new StringBuilder();
                ArrayList<Object> parameters = new ArrayList<>(3);
                getInsertSQL(command, parameters, target);
                target.ID = -1;
                if (sql.update(command.toString(), parameters.toArray()) > 0) {
                    ArrayList<Object> generatedKeys = sql.getGeneratedKeys();
                    target.ID = ((BigInteger) generatedKeys.get(0)).intValue();
                }
                return target.ID;

            }

            public boolean delete(String where, Object... parameters) throws Exception {
                if (StringExtension.isNullOrOnlyWhiteSpace(where)) {
                    throw new Exception("WHERE must not null or only white space");
                }
                return sql.update("DELETE FROM `User` " + where, parameters) > 0;
            }

            public boolean delete(int ID) throws Exception {
                return delete("WHERE `ID`=@p0", ID);
            }

            public void getUpdateSQL(StringBuilder command, ArrayList<Object> parameters, String where, tum0r.model.database.User target, tum0r.generate_code.database.enums.UserFields... fields) throws Exception {
                command.append("UPDATE `User` SET ");
                if (fields == null || fields.length == 0) {
                    fields = tum0r.generate_code.database.enums.UserFields.getAllArray();
                }

                GetFields getFields = new GetFields();
                int fieldCount = fields.length - 1;
                for (int i = 0; i <= fieldCount; i++) {
                    tum0r.generate_code.database.enums.UserFields field = fields[i];
                    command.append("`").append(field.name()).append("`").append("=").append("@p").append(parameters.size());
                    if (i != fieldCount) {
                        command.append(", ");
                    }
                    Class<?> targetFieldClass = getFields.getField(target.getClass(), field.name()).getType();
                    Object value = getFields.getFieldValue(target, field.name());
                    if (targetFieldClass.isEnum()) {
                        if (value != null) {
                            value = ((Enum) value).ordinal();
                        } else {
                            value = 0;
                        }
                    }
                    parameters.add(value);
                }
                boolean whereFlag = StringExtension.isNotNullOrEmpty(where);
                command.append(String.format("%s%s", whereFlag ? " " : "", whereFlag ? where : "")).append(";");
            }

            public boolean update(tum0r.model.database.User target, String where, tum0r.generate_code.database.enums.UserFields... fields) throws Exception {
                if (StringExtension.isNullOrOnlyWhiteSpace(where)) {
                    throw new Exception("where不能为null或空字符串");
                }
                StringBuilder command = new StringBuilder();
                ArrayList<Object> parameters = new ArrayList<>();
                getUpdateSQL(command, parameters, where, target, fields);
                return sql.update(command.toString(), parameters.toArray()) > 0;
            }

            public boolean update(tum0r.model.database.User target, tum0r.generate_code.database.enums.UserFields... fields) throws Exception {
                return update(target, "`ID`=" + target.ID, fields);
            }

            public boolean exist(String where, Object... parameters) throws Exception {
                return sql.selectValue("SELECT EXISTS (SELECT 1 FROM `User` " + where + ")", Boolean.class, parameters);
            }

            public boolean exist(int ID) throws Exception {
                return exist("WHERE `ID`=@p0", ID);
            }

            public ArrayList<tum0r.model.database.User> query(tum0r.generate_code.database.enums.UserFields[] fields, String where, Object... parameters) throws Exception {
                StringBuilder command = new StringBuilder();
                command.append("SELECT ");
                if (fields == null || fields.length == 0) {
                    fields = tum0r.generate_code.database.enums.UserFields.getAllArray();
                }
                int count = fields.length - 1;
                for (int i = 0; i <= count; i++) {
                    command.append(String.format("`%s`", fields[i].name()));
                    if (i != count) {
                        command.append(", ");
                    }
                }
                command.append(" FROM `User`");
                if (StringExtension.isNotNullOrOnlyWhiteSpace(where)) {
                    command.append(" ").append(where);
                }
                command.append(";");
                ArrayList<tum0r.model.database.User> result = sql.selectObjects(command.toString(), tum0r.model.database.User.class, parameters);
                return result != null ? result : new ArrayList<>();
            }

            public tum0r.model.database.User queryFirst(tum0r.generate_code.database.enums.UserFields[] fields, String where, Object... parameters) throws Exception {
                ArrayList<tum0r.model.database.User> temp = query(fields, where, parameters);
                return temp.size() > 0 ? temp.get(0) : null;
            }

            public tum0r.model.database.User queryByID(tum0r.generate_code.database.enums.UserFields[] fields, int ID) throws Exception {
                ArrayList<tum0r.model.database.User> result = query(fields, "WHERE `ID`=@p0", ID);
                return result.size() != 0 ? result.get(0) : null;
            }
        }

        public News _News = new News();

        public static class News {
            public void getInsertSQL(StringBuilder command, ArrayList<Object> parameters, tum0r.model.database.News target) {
                command.append("INSERT INTO `News`(`ID`, `Title`, `Content`, `Time`) VALUES(");
                for (int i = 0; i <= 3; i++) {
                    command.append("@p").append(parameters.size() + i);
                    if (i != 3) {
                        command.append(", ");
                    }
                }
                command.append(");\n");
                parameters.add(target.ID);
                parameters.add(target.Title);
                parameters.add(target.Content);
                parameters.add(target.Time);
            }

            public int insert(tum0r.model.database.News target) throws Exception {
                StringBuilder command = new StringBuilder();
                ArrayList<Object> parameters = new ArrayList<>(4);
                getInsertSQL(command, parameters, target);
                target.ID = -1;
                if (sql.update(command.toString(), parameters.toArray()) > 0) {
                    ArrayList<Object> generatedKeys = sql.getGeneratedKeys();
                    target.ID = ((BigInteger) generatedKeys.get(0)).intValue();
                }
                return target.ID;

            }

            public boolean delete(String where, Object... parameters) throws Exception {
                if (StringExtension.isNullOrOnlyWhiteSpace(where)) {
                    throw new Exception("WHERE must not null or only white space");
                }
                return sql.update("DELETE FROM `News` " + where, parameters) > 0;
            }

            public boolean delete(int ID) throws Exception {
                return delete("WHERE `ID`=@p0", ID);
            }

            public void getUpdateSQL(StringBuilder command, ArrayList<Object> parameters, String where, tum0r.model.database.News target, tum0r.generate_code.database.enums.NewsFields... fields) throws Exception {
                command.append("UPDATE `News` SET ");
                if (fields == null || fields.length == 0) {
                    fields = tum0r.generate_code.database.enums.NewsFields.getAllArray();
                }

                GetFields getFields = new GetFields();
                int fieldCount = fields.length - 1;
                for (int i = 0; i <= fieldCount; i++) {
                    tum0r.generate_code.database.enums.NewsFields field = fields[i];
                    command.append("`").append(field.name()).append("`").append("=").append("@p").append(parameters.size());
                    if (i != fieldCount) {
                        command.append(", ");
                    }
                    Class<?> targetFieldClass = getFields.getField(target.getClass(), field.name()).getType();
                    Object value = getFields.getFieldValue(target, field.name());
                    if (targetFieldClass.isEnum()) {
                        if (value != null) {
                            value = ((Enum) value).ordinal();
                        } else {
                            value = 0;
                        }
                    }
                    parameters.add(value);
                }
                boolean whereFlag = StringExtension.isNotNullOrEmpty(where);
                command.append(String.format("%s%s", whereFlag ? " " : "", whereFlag ? where : "")).append(";");
            }

            public boolean update(tum0r.model.database.News target, String where, tum0r.generate_code.database.enums.NewsFields... fields) throws Exception {
                if (StringExtension.isNullOrOnlyWhiteSpace(where)) {
                    throw new Exception("where不能为null或空字符串");
                }
                StringBuilder command = new StringBuilder();
                ArrayList<Object> parameters = new ArrayList<>();
                getUpdateSQL(command, parameters, where, target, fields);
                return sql.update(command.toString(), parameters.toArray()) > 0;
            }

            public boolean update(tum0r.model.database.News target, tum0r.generate_code.database.enums.NewsFields... fields) throws Exception {
                return update(target, "`ID`=" + target.ID, fields);
            }

            public boolean exist(String where, Object... parameters) throws Exception {
                return sql.selectValue("SELECT EXISTS (SELECT 1 FROM `News` " + where + ")", Boolean.class, parameters);
            }

            public boolean exist(int ID) throws Exception {
                return exist("WHERE `ID`=@p0", ID);
            }

            public ArrayList<tum0r.model.database.News> query(tum0r.generate_code.database.enums.NewsFields[] fields, String where, Object... parameters) throws Exception {
                StringBuilder command = new StringBuilder();
                command.append("SELECT ");
                if (fields == null || fields.length == 0) {
                    fields = tum0r.generate_code.database.enums.NewsFields.getAllArray();
                }
                int count = fields.length - 1;
                for (int i = 0; i <= count; i++) {
                    command.append(String.format("`%s`", fields[i].name()));
                    if (i != count) {
                        command.append(", ");
                    }
                }
                command.append(" FROM `News`");
                if (StringExtension.isNotNullOrOnlyWhiteSpace(where)) {
                    command.append(" ").append(where);
                }
                command.append(";");
                ArrayList<tum0r.model.database.News> result = sql.selectObjects(command.toString(), tum0r.model.database.News.class, parameters);
                return result != null ? result : new ArrayList<>();
            }

            public tum0r.model.database.News queryFirst(tum0r.generate_code.database.enums.NewsFields[] fields, String where, Object... parameters) throws Exception {
                ArrayList<tum0r.model.database.News> temp = query(fields, where, parameters);
                return temp.size() > 0 ? temp.get(0) : null;
            }

            public tum0r.model.database.News queryByID(tum0r.generate_code.database.enums.NewsFields[] fields, int ID) throws Exception {
                ArrayList<tum0r.model.database.News> result = query(fields, "WHERE `ID`=@p0", ID);
                return result.size() != 0 ? result.get(0) : null;
            }
        }

        public DAO _DAO = new DAO();

        public static class DAO {
            public <T> ArrayList<T> selectValues(String command, Class<T> c, Object... parameters) throws Exception {
                return sql.selectValues(command, c, parameters);
            }

            public <T> T selectValue(String command, Class<T> c, Object... parameters) throws Exception {
                return sql.selectValue(command, c, parameters);
            }

            public <T> ArrayList<T> selectObjects(String command, Class<T> c, Object... parameters) throws Exception {
                return sql.selectObjects(command, c, parameters);
            }

            public <T> T selectObject(String command, Class<T> c, Object... parameters) throws Exception {
                return sql.selectObject(command, c, parameters);
            }

            public <T> PageModel<T> selectPage(int index, int pageSize, Class<T> c, String command, Object... parameters) throws Exception {
                return sql.selectPage(index, pageSize, c, command, parameters);
            }

            public <T> PageModel<T> selectPage(int index, Class<T> c, String command, Object... parameters) throws Exception {
                return selectPage(index, 15, c, command, parameters);
            }

            /**
             * @deprecated 这个方法会导致SQL注入，请使用<code>selectValues(String command, Class<T> c, Object... parameters)</code>
             */
            public <T> ArrayList<T> selectValuesUnsafe(String command, Class<T> c) throws Exception {
                return sql.selectValuesUnsafe(command, c);
            }

            /**
             * @deprecated 这个方法会导致SQL注入，请使用<code>selectValue(String command, Class<T> c, Object... parameters)</code>
             */
            public <T> T selectValueUnsafe(String command, Class<T> c) throws Exception {
                return sql.selectValueUnsafe(command, c);
            }

            /**
             * @deprecated 这个方法会导致SQL注入，请使用<code>selectObjects(String command, Class<T> c, Object... parameters)</code>
             */
            public <T> ArrayList<T> selectObjectsUnsafe(String command, Class<T> c) throws Exception {
                return sql.selectObjectsUnsafe(command, c);
            }

            /**
             * @deprecated 这个方法会导致SQL注入，请使用<code>selectObject(String command, Class<T> c, Object... parameters)</code>
             */
            public <T> T selectObjectUnsafe(String command, Class<T> c) throws Exception {
                return sql.selectObjectUnsafe(command, c);
            }
        }
    }

    public static Read _Read;

    public static class Read {
        private static MySQL sql;

        public Read() {
            if (DatabaseConfig.CONNECT_READ_DATABASE) {
                try {
                    sql = new MySQL(DatabaseConfig.DATABASE_VERSION);
                    if (sql.open(DatabaseConfig.READ_HOST, DatabaseConfig.READ_PORT, DatabaseConfig.DATABASE_NAME, DatabaseConfig.READ_USERNAME, DatabaseConfig.READ_PASSWORD)) {
                        Log.info("Read 数据库连接成功");
                    } else {
                        DatabaseConfig.CONNECT_READ_DATABASE = false;
                        Log.error("Read 数据库连接失败");
                    }
                } catch (SQLException | ClassNotFoundException throwable) {
                    Log.error("Read 数据库连接崩溃");
                    Log.error(throwable);
                }
            }
        }

        public F1ag _F1ag = new F1ag();

        public static class F1ag {
            public void getInsertSQL(StringBuilder command, ArrayList<Object> parameters, tum0r.model.database.F1ag target) {
                command.append("INSERT INTO `F1ag`(`ProblemID`, `Fla9`) VALUES(");
                for (int i = 0; i <= 1; i++) {
                    command.append("@p").append(parameters.size() + i);
                    if (i != 1) {
                        command.append(", ");
                    }
                }
                command.append(");\n");
                parameters.add(target.ProblemID);
                parameters.add(target.Fla9);
            }

            public boolean insert(tum0r.model.database.F1ag target) throws Exception {
                StringBuilder command = new StringBuilder();
                ArrayList<Object> parameters = new ArrayList<>(2);
                getInsertSQL(command, parameters, target);
                return sql.update(command.toString(), parameters.toArray()) > 0;
            }

            public boolean delete(String where, Object... parameters) throws Exception {
                if (StringExtension.isNullOrOnlyWhiteSpace(where)) {
                    throw new Exception("WHERE must not null or only white space");
                }
                return sql.update("DELETE FROM `F1ag` " + where, parameters) > 0;
            }

            public boolean delete(int ProblemID) throws Exception {
                return delete("WHERE `ProblemID`=@p0", ProblemID);
            }

            public void getUpdateSQL(StringBuilder command, ArrayList<Object> parameters, String where, tum0r.model.database.F1ag target, tum0r.generate_code.database.enums.F1agFields... fields) throws Exception {
                command.append("UPDATE `F1ag` SET ");
                if (fields == null || fields.length == 0) {
                    fields = tum0r.generate_code.database.enums.F1agFields.getAllArray();
                }

                GetFields getFields = new GetFields();
                int fieldCount = fields.length - 1;
                for (int i = 0; i <= fieldCount; i++) {
                    tum0r.generate_code.database.enums.F1agFields field = fields[i];
                    command.append("`").append(field.name()).append("`").append("=").append("@p").append(parameters.size());
                    if (i != fieldCount) {
                        command.append(", ");
                    }
                    Class<?> targetFieldClass = getFields.getField(target.getClass(), field.name()).getType();
                    Object value = getFields.getFieldValue(target, field.name());
                    if (targetFieldClass.isEnum()) {
                        if (value != null) {
                            value = ((Enum) value).ordinal();
                        } else {
                            value = 0;
                        }
                    }
                    parameters.add(value);
                }
                boolean whereFlag = StringExtension.isNotNullOrEmpty(where);
                command.append(String.format("%s%s", whereFlag ? " " : "", whereFlag ? where : "")).append(";");
            }

            public boolean update(tum0r.model.database.F1ag target, String where, tum0r.generate_code.database.enums.F1agFields... fields) throws Exception {
                if (StringExtension.isNullOrOnlyWhiteSpace(where)) {
                    throw new Exception("where不能为null或空字符串");
                }
                StringBuilder command = new StringBuilder();
                ArrayList<Object> parameters = new ArrayList<>();
                getUpdateSQL(command, parameters, where, target, fields);
                return sql.update(command.toString(), parameters.toArray()) > 0;
            }

            public boolean update(tum0r.model.database.F1ag target, tum0r.generate_code.database.enums.F1agFields... fields) throws Exception {
                return update(target, "`ProblemID`=" + target.ProblemID, fields);
            }

            public boolean exist(String where, Object... parameters) throws Exception {
                return sql.selectValue("SELECT EXISTS (SELECT 1 FROM `F1ag` " + where + ")", Boolean.class, parameters);
            }

            public boolean exist(int ProblemID) throws Exception {
                return exist("WHERE `ProblemID`=@p0", ProblemID);
            }

            public ArrayList<tum0r.model.database.F1ag> query(tum0r.generate_code.database.enums.F1agFields[] fields, String where, Object... parameters) throws Exception {
                StringBuilder command = new StringBuilder();
                command.append("SELECT ");
                if (fields == null || fields.length == 0) {
                    fields = tum0r.generate_code.database.enums.F1agFields.getAllArray();
                }
                int count = fields.length - 1;
                for (int i = 0; i <= count; i++) {
                    command.append(String.format("`%s`", fields[i].name()));
                    if (i != count) {
                        command.append(", ");
                    }
                }
                command.append(" FROM `F1ag`");
                if (StringExtension.isNotNullOrOnlyWhiteSpace(where)) {
                    command.append(" ").append(where);
                }
                command.append(";");
                ArrayList<tum0r.model.database.F1ag> result = sql.selectObjects(command.toString(), tum0r.model.database.F1ag.class, parameters);
                return result != null ? result : new ArrayList<>();
            }

            public tum0r.model.database.F1ag queryFirst(tum0r.generate_code.database.enums.F1agFields[] fields, String where, Object... parameters) throws Exception {
                ArrayList<tum0r.model.database.F1ag> temp = query(fields, where, parameters);
                return temp.size() > 0 ? temp.get(0) : null;
            }

            public tum0r.model.database.F1ag queryByProblemID(tum0r.generate_code.database.enums.F1agFields[] fields, int ProblemID) throws Exception {
                ArrayList<tum0r.model.database.F1ag> result = query(fields, "WHERE `ProblemID`=@p0", ProblemID);
                return result.size() != 0 ? result.get(0) : null;
            }
        }

        public User _User = new User();

        public static class User {
            public void getInsertSQL(StringBuilder command, ArrayList<Object> parameters, tum0r.model.database.User target) {
                command.append("INSERT INTO `User`(`ID`, `Username`, `Password`) VALUES(");
                for (int i = 0; i <= 2; i++) {
                    command.append("@p").append(parameters.size() + i);
                    if (i != 2) {
                        command.append(", ");
                    }
                }
                command.append(");\n");
                parameters.add(target.ID);
                parameters.add(target.Username);
                parameters.add(target.Password);
            }

            public int insert(tum0r.model.database.User target) throws Exception {
                StringBuilder command = new StringBuilder();
                ArrayList<Object> parameters = new ArrayList<>(3);
                getInsertSQL(command, parameters, target);
                target.ID = -1;
                if (sql.update(command.toString(), parameters.toArray()) > 0) {
                    ArrayList<Object> generatedKeys = sql.getGeneratedKeys();
                    target.ID = ((BigInteger) generatedKeys.get(0)).intValue();
                }
                return target.ID;

            }

            public boolean delete(String where, Object... parameters) throws Exception {
                if (StringExtension.isNullOrOnlyWhiteSpace(where)) {
                    throw new Exception("WHERE must not null or only white space");
                }
                return sql.update("DELETE FROM `User` " + where, parameters) > 0;
            }

            public boolean delete(int ID) throws Exception {
                return delete("WHERE `ID`=@p0", ID);
            }

            public void getUpdateSQL(StringBuilder command, ArrayList<Object> parameters, String where, tum0r.model.database.User target, tum0r.generate_code.database.enums.UserFields... fields) throws Exception {
                command.append("UPDATE `User` SET ");
                if (fields == null || fields.length == 0) {
                    fields = tum0r.generate_code.database.enums.UserFields.getAllArray();
                }

                GetFields getFields = new GetFields();
                int fieldCount = fields.length - 1;
                for (int i = 0; i <= fieldCount; i++) {
                    tum0r.generate_code.database.enums.UserFields field = fields[i];
                    command.append("`").append(field.name()).append("`").append("=").append("@p").append(parameters.size());
                    if (i != fieldCount) {
                        command.append(", ");
                    }
                    Class<?> targetFieldClass = getFields.getField(target.getClass(), field.name()).getType();
                    Object value = getFields.getFieldValue(target, field.name());
                    if (targetFieldClass.isEnum()) {
                        if (value != null) {
                            value = ((Enum) value).ordinal();
                        } else {
                            value = 0;
                        }
                    }
                    parameters.add(value);
                }
                boolean whereFlag = StringExtension.isNotNullOrEmpty(where);
                command.append(String.format("%s%s", whereFlag ? " " : "", whereFlag ? where : "")).append(";");
            }

            public boolean update(tum0r.model.database.User target, String where, tum0r.generate_code.database.enums.UserFields... fields) throws Exception {
                if (StringExtension.isNullOrOnlyWhiteSpace(where)) {
                    throw new Exception("where不能为null或空字符串");
                }
                StringBuilder command = new StringBuilder();
                ArrayList<Object> parameters = new ArrayList<>();
                getUpdateSQL(command, parameters, where, target, fields);
                return sql.update(command.toString(), parameters.toArray()) > 0;
            }

            public boolean update(tum0r.model.database.User target, tum0r.generate_code.database.enums.UserFields... fields) throws Exception {
                return update(target, "`ID`=" + target.ID, fields);
            }

            public boolean exist(String where, Object... parameters) throws Exception {
                return sql.selectValue("SELECT EXISTS (SELECT 1 FROM `User` " + where + ")", Boolean.class, parameters);
            }

            public boolean exist(int ID) throws Exception {
                return exist("WHERE `ID`=@p0", ID);
            }

            public ArrayList<tum0r.model.database.User> query(tum0r.generate_code.database.enums.UserFields[] fields, String where, Object... parameters) throws Exception {
                StringBuilder command = new StringBuilder();
                command.append("SELECT ");
                if (fields == null || fields.length == 0) {
                    fields = tum0r.generate_code.database.enums.UserFields.getAllArray();
                }
                int count = fields.length - 1;
                for (int i = 0; i <= count; i++) {
                    command.append(String.format("`%s`", fields[i].name()));
                    if (i != count) {
                        command.append(", ");
                    }
                }
                command.append(" FROM `User`");
                if (StringExtension.isNotNullOrOnlyWhiteSpace(where)) {
                    command.append(" ").append(where);
                }
                command.append(";");
                ArrayList<tum0r.model.database.User> result = sql.selectObjects(command.toString(), tum0r.model.database.User.class, parameters);
                return result != null ? result : new ArrayList<>();
            }

            public tum0r.model.database.User queryFirst(tum0r.generate_code.database.enums.UserFields[] fields, String where, Object... parameters) throws Exception {
                ArrayList<tum0r.model.database.User> temp = query(fields, where, parameters);
                return temp.size() > 0 ? temp.get(0) : null;
            }

            public tum0r.model.database.User queryByID(tum0r.generate_code.database.enums.UserFields[] fields, int ID) throws Exception {
                ArrayList<tum0r.model.database.User> result = query(fields, "WHERE `ID`=@p0", ID);
                return result.size() != 0 ? result.get(0) : null;
            }
        }

        public News _News = new News();

        public static class News {
            public void getInsertSQL(StringBuilder command, ArrayList<Object> parameters, tum0r.model.database.News target) {
                command.append("INSERT INTO `News`(`ID`, `Title`, `Content`, `Time`) VALUES(");
                for (int i = 0; i <= 3; i++) {
                    command.append("@p").append(parameters.size() + i);
                    if (i != 3) {
                        command.append(", ");
                    }
                }
                command.append(");\n");
                parameters.add(target.ID);
                parameters.add(target.Title);
                parameters.add(target.Content);
                parameters.add(target.Time);
            }

            public int insert(tum0r.model.database.News target) throws Exception {
                StringBuilder command = new StringBuilder();
                ArrayList<Object> parameters = new ArrayList<>(4);
                getInsertSQL(command, parameters, target);
                target.ID = -1;
                if (sql.update(command.toString(), parameters.toArray()) > 0) {
                    ArrayList<Object> generatedKeys = sql.getGeneratedKeys();
                    target.ID = ((BigInteger) generatedKeys.get(0)).intValue();
                }
                return target.ID;

            }

            public boolean delete(String where, Object... parameters) throws Exception {
                if (StringExtension.isNullOrOnlyWhiteSpace(where)) {
                    throw new Exception("WHERE must not null or only white space");
                }
                return sql.update("DELETE FROM `News` " + where, parameters) > 0;
            }

            public boolean delete(int ID) throws Exception {
                return delete("WHERE `ID`=@p0", ID);
            }

            public void getUpdateSQL(StringBuilder command, ArrayList<Object> parameters, String where, tum0r.model.database.News target, tum0r.generate_code.database.enums.NewsFields... fields) throws Exception {
                command.append("UPDATE `News` SET ");
                if (fields == null || fields.length == 0) {
                    fields = tum0r.generate_code.database.enums.NewsFields.getAllArray();
                }

                GetFields getFields = new GetFields();
                int fieldCount = fields.length - 1;
                for (int i = 0; i <= fieldCount; i++) {
                    tum0r.generate_code.database.enums.NewsFields field = fields[i];
                    command.append("`").append(field.name()).append("`").append("=").append("@p").append(parameters.size());
                    if (i != fieldCount) {
                        command.append(", ");
                    }
                    Class<?> targetFieldClass = getFields.getField(target.getClass(), field.name()).getType();
                    Object value = getFields.getFieldValue(target, field.name());
                    if (targetFieldClass.isEnum()) {
                        if (value != null) {
                            value = ((Enum) value).ordinal();
                        } else {
                            value = 0;
                        }
                    }
                    parameters.add(value);
                }
                boolean whereFlag = StringExtension.isNotNullOrEmpty(where);
                command.append(String.format("%s%s", whereFlag ? " " : "", whereFlag ? where : "")).append(";");
            }

            public boolean update(tum0r.model.database.News target, String where, tum0r.generate_code.database.enums.NewsFields... fields) throws Exception {
                if (StringExtension.isNullOrOnlyWhiteSpace(where)) {
                    throw new Exception("where不能为null或空字符串");
                }
                StringBuilder command = new StringBuilder();
                ArrayList<Object> parameters = new ArrayList<>();
                getUpdateSQL(command, parameters, where, target, fields);
                return sql.update(command.toString(), parameters.toArray()) > 0;
            }

            public boolean update(tum0r.model.database.News target, tum0r.generate_code.database.enums.NewsFields... fields) throws Exception {
                return update(target, "`ID`=" + target.ID, fields);
            }

            public boolean exist(String where, Object... parameters) throws Exception {
                return sql.selectValue("SELECT EXISTS (SELECT 1 FROM `News` " + where + ")", Boolean.class, parameters);
            }

            public boolean exist(int ID) throws Exception {
                return exist("WHERE `ID`=@p0", ID);
            }

            public ArrayList<tum0r.model.database.News> query(tum0r.generate_code.database.enums.NewsFields[] fields, String where, Object... parameters) throws Exception {
                StringBuilder command = new StringBuilder();
                command.append("SELECT ");
                if (fields == null || fields.length == 0) {
                    fields = tum0r.generate_code.database.enums.NewsFields.getAllArray();
                }
                int count = fields.length - 1;
                for (int i = 0; i <= count; i++) {
                    command.append(String.format("`%s`", fields[i].name()));
                    if (i != count) {
                        command.append(", ");
                    }
                }
                command.append(" FROM `News`");
                if (StringExtension.isNotNullOrOnlyWhiteSpace(where)) {
                    command.append(" ").append(where);
                }
                command.append(";");
                ArrayList<tum0r.model.database.News> result = sql.selectObjects(command.toString(), tum0r.model.database.News.class, parameters);
                return result != null ? result : new ArrayList<>();
            }

            public tum0r.model.database.News queryFirst(tum0r.generate_code.database.enums.NewsFields[] fields, String where, Object... parameters) throws Exception {
                ArrayList<tum0r.model.database.News> temp = query(fields, where, parameters);
                return temp.size() > 0 ? temp.get(0) : null;
            }

            public tum0r.model.database.News queryByID(tum0r.generate_code.database.enums.NewsFields[] fields, int ID) throws Exception {
                ArrayList<tum0r.model.database.News> result = query(fields, "WHERE `ID`=@p0", ID);
                return result.size() != 0 ? result.get(0) : null;
            }
        }

        public DAO _DAO = new DAO();

        public static class DAO {
            public <T> ArrayList<T> selectValues(String command, Class<T> c, Object... parameters) throws Exception {
                return sql.selectValues(command, c, parameters);
            }

            public <T> T selectValue(String command, Class<T> c, Object... parameters) throws Exception {
                return sql.selectValue(command, c, parameters);
            }

            public <T> ArrayList<T> selectObjects(String command, Class<T> c, Object... parameters) throws Exception {
                return sql.selectObjects(command, c, parameters);
            }

            public <T> T selectObject(String command, Class<T> c, Object... parameters) throws Exception {
                return sql.selectObject(command, c, parameters);
            }

            public <T> PageModel<T> selectPage(int index, int pageSize, Class<T> c, String command, Object... parameters) throws Exception {
                return sql.selectPage(index, pageSize, c, command, parameters);
            }

            public <T> PageModel<T> selectPage(int index, Class<T> c, String command, Object... parameters) throws Exception {
                return selectPage(index, 15, c, command, parameters);
            }

            /**
             * @deprecated 这个方法会导致SQL注入，请使用<code>selectValues(String command, Class<T> c, Object... parameters)</code>
             */
            public <T> ArrayList<T> selectValuesUnsafe(String command, Class<T> c) throws Exception {
                return sql.selectValuesUnsafe(command, c);
            }

            /**
             * @deprecated 这个方法会导致SQL注入，请使用<code>selectValue(String command, Class<T> c, Object... parameters)</code>
             */
            public <T> T selectValueUnsafe(String command, Class<T> c) throws Exception {
                return sql.selectValueUnsafe(command, c);
            }

            /**
             * @deprecated 这个方法会导致SQL注入，请使用<code>selectObjects(String command, Class<T> c, Object... parameters)</code>
             */
            public <T> ArrayList<T> selectObjectsUnsafe(String command, Class<T> c) throws Exception {
                return sql.selectObjectsUnsafe(command, c);
            }

            /**
             * @deprecated 这个方法会导致SQL注入，请使用<code>selectObject(String command, Class<T> c, Object... parameters)</code>
             */
            public <T> T selectObjectUnsafe(String command, Class<T> c) throws Exception {
                return sql.selectObjectUnsafe(command, c);
            }
        }
    }

    private static void init() {
        if (_Write == null) {
            _Write = new Write();
        }
        if (_Read == null) {
            _Read = new Read();
        }
    }
}